<template>
  <div class="container">
    <div style="margin-bottom:20px">
      <el-button type="danger" @click="addPermission(1)">添加权限</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="组织架构">
      </el-table-column>
      <el-table-column align="center" prop="code" label="标识">
      </el-table-column>
      <el-table-column align="center" prop="description" label="描述">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-button type="text" @click="addPermission(2,row.id)">添加</el-button>
          <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
          <el-button type="text" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <PermissionDialog ref="PermissionRef" v-model="PermissionDialogVisible" :sonData="sonData" @updata="getPermissionList" />
  </div>
</template>
<script>
import PermissionDialog from "./components/PermissionDialog";
import { getPermissionListRequest, delPermissionByIdRequest } from "@/api/permission";
import { transListToTreeData } from '@/utils'
export default {
  name: 'PermissionCom',
  components: {
    PermissionDialog
  },
  data () {
    return {
      tableData: [],
      PermissionDialogVisible: false,
      sonData: {
        pid: 0,
        type: null
      },
      // updateId: ''
    }
  },
  computed: {
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList () {
      const res = await getPermissionListRequest()
      console.log(res)
      //平铺数据转换为树形数据
      this.tableData = transListToTreeData(res.data, 0)
    },
    //添加
    addPermission (type, id) {
      this.PermissionDialogVisible = true
      this.sonData.type = type
      this.sonData.pid = id
    },
    //编辑
    editPermission (id) {
      this.PermissionDialogVisible = true
      this.$refs.PermissionRef.getPermissionById(id)

    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delPermissionByIdRequest(id)
        this.getPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>
<style lang='scss'  scoped>
.container {
  padding: 20px;
}
</style>
